package LeetcodeTest_nine;
//移除链表元素

import java.util.Scanner;
public class removeElements
{
    public static void main(String[] args)
    {
        ListNode root=null;
        root=Solution.createListNode();
        root=Solution.removeElements(root,3);
        Solution.PrintNode(root);
    }
}
class Solution
{
    public static ListNode removeElements(ListNode head, int val)
    {
       ListNode FirstNode=new ListNode(1);
       FirstNode.next=head;
       ListNode p1=FirstNode;
       ListNode p2=head;
       while(p2!=null)
       {
           if(p2.val==val)
               p1.next=p2.next;
           else
               p1=p2;
           p2=p2.next;
       }
       return FirstNode.next;
    }
    public static ListNode createListNode()
    {
        Scanner reader=new Scanner(System.in);
        ListNode head=null;
        ListNode tail=null;
        head=new ListNode(2);
        tail=head;
        for(int i=0;i<5;i++)
        {
            ListNode p=new ListNode(reader.nextInt());
            tail.next=p;
            tail=tail.next;
        }
        return head;
    }
    public static void PrintNode(ListNode p)
    {
        while(p!=null)
        {
            System.out.print(p.val+" ");
            p=p.next;
        }
    }
}
class ListNode
{
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}